﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Assessment.Management.SharedComponent.Params.ManagerParam;
using Assessment.Management.SharedComponent.Constants;
using Assessment.Management.BIZ;
using Assessment.Management.SharedComponent.Entities;

namespace Assessment.Management.UI.Admin.UserControls.Statistic
{
    public partial class PollSetting : System.Web.UI.UserControl
    {
        #region events
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            BindDataToForm();
        }

        protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkAll = (CheckBox)grvListPoll.HeaderRow.FindControl("chkSelectAll");
            foreach (GridViewRow gvRow in grvListPoll.Rows)
            {
                CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
                chkSel.Checked = chkAll.Checked;
            }
        }

        protected void ddlKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlKhoa.SelectedIndex.ToString() == "0")
            {
                LoadGridViewPolls();
            }
            else
            {
                PollParam param = new PollParam(FunctionType.GetListPollByDepartment);
                param.Poll = new Poll() { DepartmentID = Convert.ToInt32(ddlKhoa.SelectedValue) };
                MainController.Provider.Execute(param);
                grvListPoll.DataSource = param.listPoll;
                grvListPoll.DataBind();
            }

        }

        //Bind data to grid
        protected void grvListPoll_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Poll poll = (Poll)e.Row.DataItem;
                Label status = (Label)e.Row.FindControl("lblState");
                switch (poll.State)
                {
                    case SMX.smx_IsEnable:
                        status.Text = "Đang hoạt động";
                        break;
                    case SMX.smx_IsNotEnable:
                        status.Text = "Không hoạt động";
                        break;
                    default:
                        throw new NotSupportedException();
                }

                HiddenField hidPollID = (HiddenField)e.Row.FindControl("hidPollID");
                hidPollID.Value = poll.ID.ToString();

                HiddenField hidVersion = (HiddenField)e.Row.FindControl("hidVersion");
                hidVersion.Value = poll.Version.ToString();

                HyperLink hypEdit = (HyperLink)e.Row.FindControl("hypEdit");
                hypEdit.NavigateUrl = string.Format(SMX.smx_PollEdit, poll.ID);

                ImageButton btnDelete = (ImageButton)e.Row.FindControl("btnDelete");
                string msg = string.Format("Bạn có chắc chắn muốn xóa sản phẩm có mã \"{0}\"?", poll.ID);
                btnDelete.CommandName = "Delete";
                btnDelete.OnClientClick = string.Format("return confirm('{0}');", msg);

                //TODO: 23/04/14 Thêm button Clear IP

            }
        }

        protected void grvListPoll_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = grvListPoll.Rows[e.RowIndex];
            HiddenField hidPollID = (HiddenField)row.FindControl("hidPollID");
            HiddenField hidVersion = (HiddenField)row.FindControl("hidVersion");
            List<Poll> lstPoll = new List<Poll>();
            lstPoll.Add(new Poll()
            {
                ID = hidPollID.Value,
                Version = int.Parse(hidVersion.Value),
                Deleted = SMX.smx_IsDeleted,
                UpdatedBy = SMX.smx_UpdatedBy,
                UpdatedDTG = DateTime.Now
            });
            DeletePolls(lstPoll);
            LoadGridViewPolls();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<Poll> lstPoll = GetSelectedItemOnGrid();
                DeletePolls(lstPoll);
                //Load lại gridview
                LoadGridViewPolls();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            lblMessage.Text = string.Empty;
            SearchPollSetting();
        }
        #endregion

        #region methods
        private void BindDataToForm()
        {
            LoadComboboxKhoa();
            LoadGridViewPolls();
        }

        private void LoadGridViewPolls()
        {
            PollParam param = new PollParam(FunctionType.GetListPoll);
            MainController.Provider.Execute(param);
            grvListPoll.DataSource = param.listPoll;
            grvListPoll.DataBind();
            hypAddNewPoll.HRef = SMX.smx_PollAdd;
        }

        private void LoadComboboxKhoa()
        {
            DepartmentParam param = new DepartmentParam(FunctionType.GetListDepartment);
            MainController.Provider.Execute(param);
            ddlKhoa.DataSource = param.listDepartment;
            ddlKhoa.DataBind();
            ddlKhoa.Items.Insert(0, new ListItem("----------Chọn Khoa----------", ""));
            ddlKhoa.SelectedIndex = 0;
        }

        private void DeletePolls(List<Poll> lstPoll)
        {
            PollParam param = new PollParam(FunctionType.DeletePoll);
            param.listPoll = lstPoll;
            MainController.Provider.Execute(param);
        }

        private void SearchPollSetting()
        {
            PollParam param = new PollParam(FunctionType.SearchPollSetting);
            Poll poll = new Poll();
            poll.TeacherName = txtTeacherName.Text.Trim();
            poll.ClassName = txtClassName.Text.Trim();
            poll.SubjectName = txtSubjectName.Text.Trim();
            param.Poll = poll;
            MainController.Provider.Execute(param);
            grvListPoll.DataSource = param.listPoll;
            grvListPoll.DataBind();
        }

        private List<Poll> GetSelectedItemOnGrid()
        {

            List<Poll> list = new List<Poll>();

            foreach (GridViewRow gvRow in grvListPoll.Rows)
            {
                CheckBox ckSelected = (CheckBox)gvRow.FindControl("chkSelect");
                if (ckSelected.Checked)
                {
                    list.Add(GetItemOnGrid(gvRow));
                }
            }
            return list;
        }

        private Poll GetItemOnGrid(GridViewRow gvRow)
        {
            Poll result = new Poll();
            HiddenField hidId = (HiddenField)gvRow.FindControl("hidPollID");
            HiddenField hidVersion = (HiddenField)gvRow.FindControl("hidVersion");
            result.ID = hidId.Value;
            result.Version = int.Parse(hidVersion.Value);
            result.Deleted = SMX.smx_IsDeleted;
            result.UpdatedBy = SMX.smx_UpdatedBy;
            result.UpdatedDTG = DateTime.Now;
            return result;
        }
        #endregion
    }
}